<?php
/**
  部门类
 */
class DepartmentAction extends CommonAction {
	public function index() {
		$this->display();
	}
	public function data() {
		$d = new DepartmentModel();
		$method = $_POST;
		switch($method['do']){
			case 'binzhong':
				R('Binzhong','data');
			break;
			case 'city':
				R('City','data');
			break;
			case 'p':
				if($method['p'] == 'menu')
					echo $d->order('sort asc')->getlist();
				else
					echo $d->order('sort asc')->getlist('dpid='.$method['p']);
			break;
			case 'treeid':
				$array =$d->where('did ='.$_POST['where'])->field('did,name,did,sort,tel,email,intro,cityid,binzhongid')->find();
				echo json_encode($array);
				break;
		}
	}
	/*添加数据*/
	public function add() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$dObj = new DepartmentModel();
			$data['name'] = $method['name'];
			$data['sort'] = $method['sort'];
			$data['tel'] = $method['tel'];
			$data['email'] = $method['email'];
			$data['intro'] = $method['intro'];
			$data['cityid'] = $method['cityid'];
			$data['binzhongid'] = $method['binzhongid'];
			if($dObj->add($data)){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}
	public function edit() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$dObj = new DepartmentModel();
			$dObj->did = $method['did'];
			$dObj->name = $method['name'];
			$dObj->sort = $method['sort'];
			$data->tel = $method['tel'];
			$dObj->email = $method['email'];
			$dObj->intro = $method['intro'];
			$dObj->cityid = $method['cityid'];
			$dObj->binzhongid = $method['binzhongid'];
			if($dObj->save()){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}
	public function delete() {
		if($this->isPost())
		{
			$method = $_POST;
			$data = array();
			$dObj = new DepartmentModel();
			$map['did'] = $method['id'];
			if($dObj->where($map)->delete()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}
}